import Vue from "vue";
import Router from "vue-router";
import home from "@/views/home";
import { handleError } from "@/api/request";
Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/login"),
    },
    {
      path: "/Apply",
      name: "Apply",
      component: () => import("@/views/createUser"),
    },
    {
      path: "",
      name: "home",
      component: home,
      children: [
        {
          path: "/login",
          name: "login",
          component: () => import("@/views/login"),
        },
        {
          path: "/home",
          name: "home",
          component: () => import("@/views/page1"),
        },

        {
          path: "/health",
          name: "health",
          component: () => import("@/views/page2"),
        },
        {
          path: "/patient-management",
          name: "patient-management",
          component: () => import("@/views/page3"),
        },
      ],
    },
  ],
});
router.beforeEach((to, from, next) => {
  // 检查用户是否已登录
  const isAuthenticated = sessionStorage.getItem("userInfo");
  // 如果用户未登录且要访问非登录页面，则跳转到登录页
  if (to.path == "/Apply") {
    next();
  } else if (!isAuthenticated && to.path !== "/login") {
    // next();

    next("/login");
    alert("JWT命令错误请重新登录");
  } else {
    next();
  }
});
export default router;
